package com.stellar.core.sender;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;

import java.util.concurrent.TimeUnit;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class SendRecord {

    private int minuteCount;

    private int hourCount;

    private int dayCount;

    private long lastMinuteTimestamp;

    private long lastHourTimestamp;

    private long lastDayTimestamp;

    public SendRecord() {

    }

    public SendRecord(long currentTime, long hourTime, long dayTime) {
        this.minuteCount = 1;
        this.hourCount = 1;
        this.dayCount = 1;
        this.lastMinuteTimestamp = currentTime;
        this.lastHourTimestamp = hourTime;
        this.lastDayTimestamp = dayTime;
    }

    public void update(long currentTime) {
        updateMinute(currentTime);
        updateHour(currentTime);
        updateDay(currentTime);
    }

    public void updateMinute(long currentTime) {
        if (currentTime - lastMinuteTimestamp > TimeUnit.MINUTES.toMillis(1)) { // 1分钟
            minuteCount = 1;
            lastMinuteTimestamp = currentTime;
        } else {
            minuteCount++;
        }
    }

    private void updateHour(long currentTime) {
        if (currentTime - lastHourTimestamp > TimeUnit.HOURS.toMillis(1)) { // 1小时
            hourCount = 1;
            lastHourTimestamp = currentTime;
        } else {
            hourCount++;
        }
    }

    private void updateDay(long currentTime) {
        if (currentTime - lastDayTimestamp > TimeUnit.DAYS.toMillis(1)) { // 1天
            dayCount = 1;
            lastDayTimestamp = currentTime;
        } else {
            dayCount++;
        }
    }
}